package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.apache.commons.cli.ParseException;
import org.junit.Test;

/**
 * Unit tests for class CommandLineArgs.
 * 
 * @author Team Purple John Ancheta Daniel Arakaki Mari-Lee Flestado
 */
public class TestCommandLineArgs {
  /** Static variable.*/
  public static final String verbose = "-verbose";
  /** Static variable.*/
  public static final String uhm = "-uhm";
  /** Static variable.*/
  public static final String hsl = "-hsl";
  /** Static variable.*/
  public static final String option1 = "option1";
  /** Static variable.*/
  public static final String option2 = "option2";
  /** Static variable.*/
  public static final String option3 = "option3";
  /** Static variable.*/
  public static final String option4 = "option4";
  /**
   * Test CommandLineArgs.parse with null args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test (expected = ParseException.class)
  public void testParseWithNull() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(null);
  }

  /**
   * Test CommandLineArgs.parse with -verbose in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithOptionVerbose() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose,
                                        uhm, option1, option2,
                                        hsl,option3,option4});
    assertTrue("Should be true testParseWithOptionVerbose.", commandLineArgs.hasVerboseOption());
  }
 
  /**
   * Test CommandLineArgs.parse with -verbose in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithOptionNoVerbose() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {uhm, option1,option2});
    assertFalse("Should be false.", commandLineArgs.hasVerboseOption());
  }
  
  /**
   * Test CommandLineArgs.parse with -uhm in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithValidUhmOptions() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {uhm, option1,option2});
    assertTrue("Should be true -uhm.", commandLineArgs.hasUhmOption());
    assertEquals("Should be the same -uhm option1", option1, commandLineArgs.getUhmId());
    assertEquals("Should be the same -uhm option2", option2, commandLineArgs.getUhmLastName());
  }
  
  /**
   * Test CommandLineArgs.parse with -hsl in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithValidHslOptions() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {hsl,option1,option2});
    assertTrue("Should be true -hsl.", commandLineArgs.hasHslOption());
    assertEquals("Should be the same -hsl option1", option1, 
                 commandLineArgs.getHslId());
    assertEquals("Should be the same -hsl option2", option2, 
                 commandLineArgs.getHslLastFourDigitsForPhoneNumber());
  }
  
  /**
   * Test CommandLineArgs.parse with -uhm and -hsl in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithValidUhmAndHslOptions() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {uhm, option1, option2,
                                        hsl,option3,option4});
    assertTrue("Should be true a.", commandLineArgs.hasUhmOption());
    assertEquals("Should be the same a", option1, commandLineArgs.getUhmId());
    assertEquals("Should be the same b", option2, commandLineArgs.getUhmLastName());
    assertTrue("Should be true b.", commandLineArgs.hasHslOption());
    assertEquals("Should be the same c", option3, 
                 commandLineArgs.getHslId());
    assertEquals("Should be the same d", option4, 
                 commandLineArgs.getHslLastFourDigitsForPhoneNumber());
  }
  
  /**
   * Test CommandLineArgs.parse with no required -uhm and/or -hsl in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test (expected = ParseException.class)
  public void testParseWithNoUhmAndHslOptions() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose});
  }
  
  /**
   * Test CommandLineArgs.parse with -sort duedates|library in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test (expected = ParseException.class)
  public void testParseWithSort() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose,
                                        "-sort", "library",
                                        uhm, option1, option2,
                                        hsl,option3,option4});
    assertTrue("Should be true c.", commandLineArgs.hasSortOption());
    assertEquals("Should be the same e", "library", commandLineArgs.getSortValue());
  }
  
  /**
   * Test CommandLineArgs.parse with -sort duedates|library in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithDefaultSort() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose,
                                        uhm, option1, option2,
                                        hsl,option3,option4});
    assertFalse("Should be false.", commandLineArgs.hasSortOption());
    assertEquals("Should be the same f", "duedates", commandLineArgs.getSortValue());
  }
  
  /**
   * Test CommandLineArgs.parse with -within integer in args.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test (expected = ParseException.class)
  public void testParseWithWithin() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose,
                                        "-within", "2",
                                        uhm, option1, option2,
                                        hsl,option3,option4});
    assertTrue("Should be true d.", commandLineArgs.hasWithinOption());
    assertEquals("Should be the same g", 2, commandLineArgs.getWithinValue());
  }
  
  /**
   * Test CommandLineArgs.parse default value for -within.
   * 
   * @throws ParseException for incorrect command line argument.
   */
  @Test
  public void testParseWithWithinDefaultValue() throws ParseException {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {verbose,
                                        uhm, option1, option2,
                                        hsl,option3,option4});
    assertFalse("Should be false.", commandLineArgs.hasWithinOption());
    assertEquals("Should be the same h", 0, commandLineArgs.getWithinValue());
  }
}
